package com.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entity.Message;
import com.example.service.MessageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "消息管理接口")
@RestController
@RequestMapping("/api/messages")
@RequiredArgsConstructor
public class MessageController {

    private final MessageService messageService;

    @Operation(summary = "发送消息")
    @PostMapping("/send")
    public Result<Boolean> send(@RequestBody Message message) {
        return Result.success(messageService.sendMessage(message));
    }

    @Operation(summary = "批量发送消息")
    @PostMapping("/batch-send")
    public Result<Boolean> batchSend(
            @RequestBody Message message,
            @Parameter(description = "接收者ID数组") @RequestParam Long[] receiverIds) {
        return Result.success(messageService.batchSendMessage(message, receiverIds));
    }

    @Operation(summary = "标记消息为已读")
    @PostMapping("/{id}/read")
    public Result<Boolean> markAsRead(@Parameter(description = "消息ID") @PathVariable Long id) {
        return Result.success(messageService.markAsRead(id));
    }

    @Operation(summary = "批量标记消息为已读")
    @PostMapping("/batch-read")
    public Result<Boolean> batchMarkAsRead(@Parameter(description = "消息ID数组") @RequestBody Long[] ids) {
        return Result.success(messageService.batchMarkAsRead(ids));
    }

    @Operation(summary = "删除消息")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@Parameter(description = "消息ID") @PathVariable Long id) {
        return Result.success(messageService.deleteMessage(id));
    }

    @Operation(summary = "批量删除消息")
    @DeleteMapping("/batch")
    public Result<Boolean> batchDelete(@Parameter(description = "消息ID数组") @RequestBody Long[] ids) {
        return Result.success(messageService.batchDeleteMessages(ids));
    }

    @Operation(summary = "分页查询消息列表")
    @GetMapping("/list")
    public Result<Page<Message>> list(
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @Parameter(description = "接收者ID") @RequestParam Long receiverId,
            @Parameter(description = "消息类型") @RequestParam(required = false) Integer type,
            @Parameter(description = "消息状态") @RequestParam(required = false) Integer status) {
        return Result.success(messageService.getMessageList(pageNum, pageSize, receiverId, type, status));
    }

    @Operation(summary = "获取未读消息数量")
    @GetMapping("/unread-count")
    public Result<Integer> getUnreadCount(@Parameter(description = "接收者ID") @RequestParam Long receiverId) {
        return Result.success(messageService.getUnreadCount(receiverId));
    }
} 